package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.AddressListResp;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.hmo.mall.views.SwipeLayout;
import xhwl.hmo.mall.views.SwipeLayoutManager;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;

public class AddressManageAdapter extends BaseAdapter<AddressListResp.Address> {
    List<AddressListResp.Address> mList;
    OnItemClickListener onItemClickListener;
    Handler handler=new Handler();
    public AddressManageAdapter(Context context, List<AddressListResp.Address> list) {
        super(context, R.layout.item_addressmanage, list);
        this.mList = list;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    @Override
    public void convert(final ViewHolder holder, AddressListResp.Address resp) {
        holder.setText(R.id.tvName, resp.SH_NAME);
        holder.setText(R.id.tvPhone, resp.SH_MOBILE);
        holder.setText(R.id.tvAddress, resp.PROVINCE_NAME+" "+resp.CITY_NAME+" "+resp.DISTRICT_NAME);
        holder.setText(R.id.tvDetailAddress,resp.SH_ADDRESS);
        final int pos = holder.getLayoutPosition();
        final SwipeLayout swipe_layout = (SwipeLayout) holder.getView(R.id.swipe_layout);
        RelativeLayout layout = (RelativeLayout) holder.getView(R.id.rlItem);

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(SwipeLayoutManager.getInstance().getSwipeLayout()!=null){//有打开的layout
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    SwipeLayoutManager.getInstance().clearSwipeLayout();
                }
                else
                {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(pos);
                    }
                }
            }
        });
        holder.getView(R.id.address_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwipeLayoutManager.getInstance().closeCurrentLayout();
                SwipeLayoutManager.getInstance().clearSwipeLayout();
//                swipe_layout.setCurrentState(SwipeLayout.STATE_CLOSE);
                //删除接口............
                if(onItemClickListener!=null){
                    onItemClickListener.onDelete(pos);
                }
            }
        });
        holder.getView(R.id.address_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwipeLayoutManager.getInstance().closeCurrentLayout();
                SwipeLayoutManager.getInstance().clearSwipeLayout();
//                swipe_layout.setCurrentState(SwipeLayout.STATE_CLOSE);
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
                        if(onItemClickListener!=null){
                            onItemClickListener.onEdit(pos);
                        }
//                    }
//                },200);
            }
        });
    }
    public interface OnItemClickListener{
        void onDelete(int position);
        void onEdit(int position);
        void onItemClick(int position);
    }
}
